package com.oa.lms.apps.actions.user;

import java.io.IOException;
import java.util.List;

import com.oa.lms.apps.actions.BasicAction;
import com.oa.lms.apps.business.user.IPostInfoService;
import com.oa.lms.apps.business.user.ISectionInfoService;
import com.oa.lms.apps.constants.IDEDStatus;
import com.oa.lms.apps.entity.user.PostInfoValue;
import com.oa.lms.apps.entity.user.SectionInfoValue;

@SuppressWarnings("serial")
public class PostInfoAction extends BasicAction {
	
	private IPostInfoService postInfoService;
	private ISectionInfoService sectionInfoService;
	private PostInfoValue postInfo;
	private List<SectionInfoValue> sectionList;
	private List<PostInfoValue> postList;
	
	private String postName;
	private String sectionId;
	private String postId;
	private String str;
	
	public String addPost(){
		
		postInfoService.add(postName,Integer.parseInt(sectionId));
		return SUCCESS;
	}
	public String toAdd(){
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStatus.ENABLE);
		return SUCCESS;
	}
	
	public String updatePost(){
		
	
		postInfoService.modify(Integer.parseInt(postId),postName,Integer.parseInt(sectionId) );
		return SUCCESS;
		
	}
	

	public String toUpdate(){
		postInfo = postInfoService.queryByPostId(Integer.parseInt(postId));
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStatus.ENABLE);
		postId = postInfo.getPostId().toString();
		postName = postInfo.getPostName();
		sectionId = postInfo.getSectionInfo().getSectionId().toString();
		
		return SUCCESS;
	}
	
	public String toQuery(){
		postList = postInfoService.queryPostInfo(null, null, IDEDStatus.ENABLE);
		return SUCCESS;
	}
	
	public String queryPostBySection() throws IOException{

		if (sectionId != null) {
			postList = postInfoService.queryPostInfo(null, Integer.parseInt(sectionId), null);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			this.str = getPostOptionsFrom(postList);
			response.getWriter().print(str);
		}
		return null;
	}
	public String toDeletePost(){
		postInfoService.delete(Integer.parseInt(postId));
		return SUCCESS;
	}

	
	private String getPostOptionsFrom(List<PostInfoValue> list) {
		StringBuilder sb = new StringBuilder();
		sb.append("<option value=\"-1\">-请选择-</option>");
		for (PostInfoValue piv : list) {
			String temp = "<option value=\"" + piv.getPostId().toString()
					+ "\">" + piv.getPostName() + "</option>";
			sb.append(temp);
		}
		return sb.toString();
	}
	
	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}
	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}
	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}
	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}
	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}
	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}
	public String getPostName() {
		return postName;
	}
	public void setPostName(String postName) {
		this.postName = postName;
	}
	public String getSectionId() {
		return sectionId;
	}
	public void setSectionId(String sectionId) {
		this.sectionId = sectionId;
	}
	public List<PostInfoValue> getPostList() {
		return postList;
	}
	public void setPostList(List<PostInfoValue> postList) {
		this.postList = postList;
	}
	public String getPostId() {
		return postId;
	}
	public void setPostId(String postId) {
		this.postId = postId;
	}
	public PostInfoValue getPostInfo() {
		return postInfo;
	}
	public void setPostInfo(PostInfoValue postInfo) {
		this.postInfo = postInfo;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}


	
	
}
